package com.lei.study.mvel.high;

import org.mvel2.MVEL;
import org.mvel2.ParserContext;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolverFactory;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 伍磊
 */
public class Parse {

    /**
     * 定义add
     */
    private static final String add = "def add(a, b) { return a + b; }";

    private static final Map<String, Object> map = new HashMap<>();

    public static void main(String[] args) {



        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(add);

        map.put("REV", "0.018000000");
        map.put("REVF", "0.016000000");
        map.put("REVZ", "4");

        ParserContext parserContext = new ParserContext();

        Serializable serializable = MVEL.compileExpression(stringBuilder.append("REV - REVF").toString());
        VariableResolverFactory variableResolverFactory = new MapVariableResolverFactory(map);
        Object o = MVEL.executeExpression(serializable, map);
        System.out.println(o);
    }
}
